<style>
    html {
        background-color: #ffffff;
    }
</style>
<div class="form-psw" style="background-color: #ffffff;">
    <form id="form-psw" class="layui-form model-form" method="post"
          lay-filter="user-admin-filter">
        <div class="layui-form-item">
            <label class="layui-form-label">原始密码:</label>
            <div class="layui-input-block">
                <input type="password" name="old_password" placeholder="请输入原始密码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码:</label>
            <div class="layui-input-block">
                <input type="password" name="new_password" placeholder="请输入新密码" class="layui-input"
                       lay-verType="tips" lay-verify="required|psw" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码:</label>
            <div class="layui-input-block">
                <input type="password" name="rep_password" placeholder="请再次输入新密码" class="layui-input"
                       lay-verType="tips" lay-verify="required|repsw" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="submit-psw" lay-submit>保存</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['layer', 'form', 'admin', 'index'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var index = layui.index;

        admin.iframeAuto();  // 让当前iframe弹层高度适应

        // 监听提交
        form.on('submit(submit-psw)', function (data) {
            layer.load(2);
            admin.req('/admin/user/update-password', data.field, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg('操作成功', {icon: 1}, function () {
                        // 关闭当前iframe弹出层
                        admin.closeThisDialog();
                        index.clearTabCache();
                        top.window.location.href = '/user/logout';
                    });
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            }, 'post');

            return false;
        });

        // 添加表单验证方法
        form.verify({
            psw: [/^[\S]{5,12}$/, '密码必须5到12位，且不能出现空格'],
            repsw: function (t) {
                if (t !== $('#form-psw input[name=new_password]').val()) {
                    return '两次密码输入不一致';
                }
            }
        });
    });
</script>